package cn.lls.service.impl;

import cn.lls.base.common.LlsConstant;
import cn.lls.base.exception.LlsException;
import cn.lls.base.result.ResultCode;
import cn.lls.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 邮件服务实现类
 *
 * @ClassName EmailServiceImpl
 * @Author 划水艺术家
 * @Date 2022/11/20 2:37
 * @Version 1.0
 **/
@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    private void sendContext(String title, String context, String toEmail) throws MessagingException {

        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(LlsConstant.EMAIL_FROM + "(六六顺)");
        helper.setTo(toEmail);
        helper.setSubject(title);
        helper.setText(context, true);
        javaMailSender.send(message);
    }


    @Override
    public void sendCode(String code, String toEmail) {
        String title = "【六六顺】| 验证码";
        try {
            sendContext(title, getSendCodeTemplate(code), toEmail);
        } catch (Exception e) {
            throw new LlsException(ResultCode.CODE_SEND_FAIL);
        }
    }

    @Override
    public void sendAgreeMessage(String companyName, String account, String loginPassword, String toEmail) {
        String title = "【六六顺】| 审核成功";
        try {
            sendContext(title, getSendAgreeTemplate(companyName, account, loginPassword), toEmail);
        } catch (Exception e) {
            throw new LlsException(ResultCode.CODE_SEND_FAIL);
        }
    }

    @Override
    public void sendRefuseMessage(String companyName, String message, String toEmail) {
        String title = "【六六顺】| 审核失败";
        try {
            sendContext(title, getSendRefuseTemplate(companyName, message), toEmail);
        } catch (Exception e) {
            throw new LlsException(ResultCode.CODE_SEND_FAIL);
        }
    }

    @Override
    public void sendBannedMessage(String companyName, String message, String toEmail) {
        String title = "【六六顺】| 封禁通知";
        try {
            sendContext(title, getSendBannedTemplate(companyName, message), toEmail);
        } catch (Exception e) {
            throw new LlsException(ResultCode.CODE_SEND_FAIL);
        }
    }

    @Override
    public void sendUnblockMessage(String companyName, String toEmail) {
        String title = "【六六顺】| 解封通知";
        try {
            sendContext(title, getSendUnblockTemplate(companyName), toEmail);
        } catch (Exception e) {
            throw new LlsException(ResultCode.CODE_SEND_FAIL);
        }
    }

    /*
        获取发送验证码的模板
     */
    private String getSendCodeTemplate(String code){

        return "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "  <table cellpadding=\"0\" align=\"center\"\n" +
                "         style=\"width: 600px;height: 100%; margin: 0px auto; text-align: left; position: relative;\n" +
                "          border-top-left-radius: 5px; border-top-right-radius: 5px;\n" +
                "          border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;\n" +
                "          font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5;\n" +
                "          box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "      <th valign=\"middle\"\n" +
                "          style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px;\n" +
                "           border-bottom-style: solid; border-bottom-color: #378dee;\n" +
                "           background-color: #378dee; border-top-left-radius: 5px;\n" +
                "            border-top-right-radius: 5px; border-bottom-right-radius: 0px;\n" +
                "            border-bottom-left-radius: 0px;\">\n" +
                "        <span style=\"color: rgb(255, 255, 255);  font-family: 微软雅黑; font-size: large; text-align: center\">六六顺</span>\n" +
                "      </th>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"word-break:break-all\">\n" +
                "        <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "          <h2 style=\"margin: 5px 0px; \">\n" +
                "            <span style=\"line-height: 20px;  color: #333333; \">\n" +
                "              <span style=\"line-height: 22px;  font-size: medium; \">\n" +
                "                尊敬的用户：</span>\n" +
                "            </span>\n" +
                "          </h2>\n" +
                "          <p style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;您好！感谢您使用六六顺人事管理系统，您的账号正在进行邮箱验证，验证码为：\n" +
                "            <span style=\"color: #4a9cec; font-size:24px;\"> " + code + "&nbsp;</span>，有效期5分钟，请尽快填写验证码完成验证！\n" +
                "            <span style=\"font-size:16px;\">（请勿泄漏此验证码，如非本人操作，请忽略该邮件）</span>\n" +
                "          </p>\n" +
                "          <br>\n" +
                "          <br>\n" +
                "\n" +
                "          <div style=\"width:100%;margin:0 auto;\">\n" +
                "            <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "              <p>六六顺官方</p>\n" +
                "              <p>联系我们：2731695853@qq.com</p>\n" +
                "              <br>\n" +
                "              <p>此为系统邮件，请勿回复<br>\n" +
                "                Please do not reply to this system email\n" +
                "              </p>\n" +
                "              <br>\n" +
                "              <p>\n" +
                "                " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "\n" +
                "              </p>\n" +
                "            </div>\n" +
                "          </div>\n" +
                "        </div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "  </table>\n" +
                "</div>";
    }

    /*
       获取发送审核通过的模板
    */
    private String getSendAgreeTemplate(String companyName, String account, String loginPassword){
        return "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "  <table cellpadding=\"0\" align=\"center\"\n" +
                "         style=\"width: 600px;height: 100%; margin: 0px auto; text-align: left; position: relative;\n" +
                "          border-top-left-radius: 5px; border-top-right-radius: 5px;\n" +
                "          border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;\n" +
                "          font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5;\n" +
                "          box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "      <th valign=\"middle\"\n" +
                "          style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px;\n" +
                "           border-bottom-style: solid; border-bottom-color: #378dee;\n" +
                "           background-color: #378dee; border-top-left-radius: 5px;\n" +
                "            border-top-right-radius: 5px; border-bottom-right-radius: 0px;\n" +
                "            border-bottom-left-radius: 0px;\">\n" +
                "        <span style=\"color: rgb(255, 255, 255);  font-family: 微软雅黑; font-size: large; text-align: center\">六六顺</span>\n" +
                "      </th>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"word-break:break-all\">\n" +
                "        <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "          <h2 style=\"margin: 5px 0px; \">\n" +
                "            <span style=\"line-height: 20px;  color: #333333; \">\n" +
                "              <span style=\"line-height: 22px;  font-size: medium; \">\n" +
                "                " + companyName + "：</span>\n" +
                "            </span>\n" +
                "          </h2>\n" +
                "          <p style=\"font-size:16px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;贵公司已通过六六顺人事管理系统审核,可开始使用,现将公司管理员账号密码告知\n" +
                "          ,请妥善保管,登录系统后请立即更改密码！\n" +
                "          <br>\n" +
                "            <h5 style=\"font-size: 15px; \">\n" +
                "               <span> 公司管理员账号可设置公司部门、职位并分配权限。</span>\n" +
                "              <br>\n" +
                "               <span style=\"color: #dd0f0f\">  注: 各公司只存在一个公司管理员账号,请妥善保管!</span>\n" +
                "               </h5>\n" +
                "          <ul>\n" +
                "            <li> <h3 > 账号: " + account + "</h3></li>\n" +
                "            <li> <h3 > 密码: " + loginPassword + "</h3></li>\n" +
                "          </ul>\n" +
                "          <a target=\"_blank\" href=\"http://tangyt.cn\" style=\"text-decoration: none\">系统登录</a>\n" +
                "          </p>\n" +
                "\n" +
                "          <br>\n" +
                "          <br>\n" +
                "\n" +
                "          <div style=\"width:100%;margin:0 auto;\">\n" +
                "            <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "              <p>六六顺官方</p>\n" +
                "              <p>联系我们：2731695853@qq.com</p>\n" +
                "              <br>\n" +
                "              <p>此为系统邮件，请勿回复<br>\n" +
                "                Please do not reply to this system email\n" +
                "              </p>\n" +
                "              <br>\n" +
                "              <p>\n" +
                "                " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "\n" +
                "              </p>\n" +
                "            </div>\n" +
                "          </div>\n" +
                "        </div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "  </table>\n" +
                "</div>";
    }

    /*
       获取发送审核失败的模板
    */
    private String getSendRefuseTemplate(String companyName, String message){
        return "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "  <table cellpadding=\"0\" align=\"center\"\n" +
                "         style=\"width: 600px;height: 100%; margin: 0px auto; text-align: left; position: relative;\n" +
                "          border-top-left-radius: 5px; border-top-right-radius: 5px;\n" +
                "          border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;\n" +
                "          font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5;\n" +
                "          box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "      <th valign=\"middle\"\n" +
                "          style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px;\n" +
                "           border-bottom-style: solid; border-bottom-color: #378dee;\n" +
                "           background-color: #378dee; border-top-left-radius: 5px;\n" +
                "            border-top-right-radius: 5px; border-bottom-right-radius: 0px;\n" +
                "            border-bottom-left-radius: 0px;\">\n" +
                "        <span style=\"color: rgb(255, 255, 255);  font-family: 微软雅黑; font-size: large; text-align: center\">六六顺</span>\n" +
                "      </th>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"word-break:break-all\">\n" +
                "        <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "          <h2 style=\"margin: 5px 0px; \">\n" +
                "            <span style=\"line-height: 20px;  color: #333333; \">\n" +
                "              <span style=\"line-height: 22px;  font-size: medium; \">\n" +
                "                " + companyName + "：</span>\n" +
                "            </span>\n" +
                "          </h2>\n" +
                "          <h3 style=\"font-size:16px; color:#ec4c4c;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;贵公司未通过六六顺人事管理系统审核,请查看原因并重新提交审核！\n" +
                "          </h3>\n" +
                "          <p>\n" +
                "            <h5 style=\"font-size: 13px;\">审核未通过原因如下:</h5>\n" +
                "            <span>" + message + "</span>\n" +
                "          </p>\n" +
                "          <a target=\"_blank\" href=\"http://tangyt.cn\" style=\"text-decoration: none\">点击这里修改审核信息</a>\n" +
                "\n" +
                "          <br>\n" +
                "          <br>\n" +
                "\n" +
                "          <div style=\"width:100%;margin:0 auto;\">\n" +
                "            <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "              <p>六六顺官方</p>\n" +
                "              <p>联系我们：2731695853@qq.com</p>\n" +
                "              <br>\n" +
                "              <p>此为系统邮件，请勿回复<br>\n" +
                "                Please do not reply to this system email\n" +
                "              </p>\n" +
                "              <br>\n" +
                "              <p>\n" +
                "                " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "\n" +
                "              </p>\n" +
                "            </div>\n" +
                "          </div>\n" +
                "        </div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "  </table>\n" +
                "</div>";
    }

    /*
       获取发送公司被封禁的模板
    */
    private String getSendBannedTemplate(String companyName, String message){
        return "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "  <table cellpadding=\"0\" align=\"center\"\n" +
                "         style=\"width: 600px;height: 100%; margin: 0px auto; text-align: left; position: relative;\n" +
                "          border-top-left-radius: 5px; border-top-right-radius: 5px;\n" +
                "          border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;\n" +
                "          font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5;\n" +
                "          box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "      <th valign=\"middle\"\n" +
                "          style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px;\n" +
                "           border-bottom-style: solid; border-bottom-color: #378dee;\n" +
                "           background-color: #378dee; border-top-left-radius: 5px;\n" +
                "            border-top-right-radius: 5px; border-bottom-right-radius: 0px;\n" +
                "            border-bottom-left-radius: 0px;\">\n" +
                "        <span style=\"color: rgb(255, 255, 255);  font-family: 微软雅黑; font-size: large; text-align: center\">六六顺</span>\n" +
                "      </th>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"word-break:break-all\">\n" +
                "        <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "          <h2 style=\"margin: 5px 0px; \">\n" +
                "            <span style=\"line-height: 20px;  color: #333333; \">\n" +
                "              <span style=\"line-height: 22px;  font-size: medium; \">\n" +
                "                " + companyName + "：</span>\n" +
                "            </span>\n" +
                "          </h2>\n" +
                "          <h3 style=\"font-size:16px; color:#ec4c4c;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;贵公司被禁止使用本系统,请查看原因并重新提交审核以解封！\n" +
                "          </h3>\n" +
                "          <p>\n" +
                "            <h5 style=\"font-size: 13px;\">封禁原因如下:</h5>\n" +
                "            <span>" + message + "</span>\n" +
                "          </p>\n" +
                "          <a target=\"_blank\" href=\"http://tangyt.cn\" style=\"text-decoration: none\">点击这里修改审核信息</a>\n" +
                "\n" +
                "          <br>\n" +
                "          <br>\n" +
                "\n" +
                "          <div style=\"width:100%;margin:0 auto;\">\n" +
                "            <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "              <p>六六顺官方</p>\n" +
                "              <p>联系我们：2731695853@qq.com</p>\n" +
                "              <br>\n" +
                "              <p>此为系统邮件，请勿回复<br>\n" +
                "                Please do not reply to this system email\n" +
                "              </p>\n" +
                "              <br>\n" +
                "              <p>\n" +
                "                " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "\n" +
                "              </p>\n" +
                "            </div>\n" +
                "          </div>\n" +
                "        </div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "  </table>\n" +
                "</div>";
    }

    /*
       获取发送公司解封的模板
    */
    private String getSendUnblockTemplate(String companyName){
        return "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "  <table cellpadding=\"0\" align=\"center\"\n" +
                "         style=\"width: 600px;height: 100%; margin: 0px auto; text-align: left; position: relative;\n" +
                "          border-top-left-radius: 5px; border-top-right-radius: 5px;\n" +
                "          border-bottom-right-radius: 5px; border-bottom-left-radius: 5px;\n" +
                "          font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5;\n" +
                "          box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "      <th valign=\"middle\"\n" +
                "          style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px;\n" +
                "           border-bottom-style: solid; border-bottom-color: #378dee;\n" +
                "           background-color: #378dee; border-top-left-radius: 5px;\n" +
                "            border-top-right-radius: 5px; border-bottom-right-radius: 0px;\n" +
                "            border-bottom-left-radius: 0px;\">\n" +
                "        <span style=\"color: rgb(255, 255, 255);  font-family: 微软雅黑; font-size: large; text-align: center\">六六顺</span>\n" +
                "      </th>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "      <td style=\"word-break:break-all\">\n" +
                "        <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "          <h2 style=\"margin: 5px 0px; \">\n" +
                "            <span style=\"line-height: 20px;  color: #333333; \">\n" +
                "              <span style=\"line-height: 22px;  font-size: medium; \">\n" +
                "                " + companyName + "：</span>\n" +
                "            </span>\n" +
                "          </h2>\n" +
                "          <h3 style=\"font-size:16px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;贵公司已解封,可继续使用本系统。【特此通知】！\n" +
                "          </h3>\n" +
                "          <a target=\"_blank\" href=\"http://tangyt.cn\" style=\"text-decoration: none\">点击这里登录系统</a>\n" +
                "\n" +
                "          <br>\n" +
                "          <br>\n" +
                "\n" +
                "          <div style=\"width:100%;margin:0 auto;\">\n" +
                "            <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "              <p>六六顺官方</p>\n" +
                "              <p>联系我们：2731695853@qq.com</p>\n" +
                "              <br>\n" +
                "              <p>此为系统邮件，请勿回复<br>\n" +
                "                Please do not reply to this system email\n" +
                "              </p>\n" +
                "              <br>\n" +
                "              <p>\n" +
                "                " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "\n" +
                "              </p>\n" +
                "            </div>\n" +
                "          </div>\n" +
                "        </div>\n" +
                "      </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "  </table>\n" +
                "</div>";
    }
}
